/**
 * This file is part of iSecurePasswords.
 *
 *   iSecurePasswords is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   iSecurePasswords is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with iSecurePasswords.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Matthew MacGregor
 * Created: 2012
 * iSecurePasswords, copyright 2013 Sudolink, LLC
 * 
 */ 
package com.sudolink.isp.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;

/**
 *
 * @author dev
 */
public class HelperMethods {

    public enum URLState {

        VALID,
        MALFORMED,
        DISCONNECTED
    }

    public static URLState isValidUrl(String url) {
        try {
            URL _url = new URL(url);
            URLConnection conn = _url.openConnection();
            conn.connect();
            return URLState.VALID;
        } catch (MalformedURLException e) {
            return URLState.MALFORMED;
        } catch (IOException e) {
            return URLState.DISCONNECTED;
        }
    }

    public static byte[] generateSalt() {
        long time = System.currentTimeMillis();
        return ByteBuffer.allocate(8).putLong(time).array();
    }
    
    /**
     * Utility function to convert seconds into milliseconds.
     * @param seconds
     * @return 
     */
    public static long secondsToMilliseconds( long seconds ) {
        return seconds * 1000;
    }
    
    /**
     * Utility function to convert milliseconds into seconds.
     * @param milliseconds 
     * @return Number of seconds.
     */
    public static long millisecondsToSeconds( long milliseconds ) {
        return milliseconds / 1000;
    }
}
